# Virtual Router Simulator 
#
# Copyright (C) 2009-2010 VRSim Project
# Author: Choonho Son <choonho@kt.com>
# URL: <http://vrsim.googlecode.com>
# For license information, see LICENSE.TXT
#

import xmlrpclib
import VRSim.Globals as globals
from VRSim.Utils import debug

class VMAPIClient:
    def __init__(self,  nodeid):
        self.nodeid = nodeid
        debug("VMAPI Node ID:" + str(self.nodeid))
        
    def getConsolePort(self):
        debug("VMAPIClient Node ID:" + str(self.nodeid))
        vmname = globals.GApp.topology.nodes[self.nodeid].vm
        hypervisors = globals.GApp.topology.hypervisors
        
        for item in hypervisors:
            debug(item[0] + " vs. " + vmname,  "VMAPIClient")
            if item[0] == vmname:
                self.vmapi_uri = item[3]
                
        
        try:
            s = xmlrpclib.ServerProxy(self.vmapi_uri)
            ret = s.getConsolePort()
            return ret
        except:
            globals.err = "Cannot Connect VMAPI RPC Server"
            globals.GApp.mainWindow.NodesLog.addError(globals.err)
            debug("Cannot connect RPC: " + self.vmapi_uri )
    
    
